package BBDD;

import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import DAO.AlumnoDAO;

public class ManageAlumno {

	private Connection conn;
	private Statement pstmt;
	private ResultSet rs;
	private String query;
	private ManageDB abreConn;
	private Vector<AlumnoDAO> alumnos= new Vector<AlumnoDAO>();
	
	public ManageAlumno() {
		abreConn = new ManageDB();
		conn = abreConn.getConnection();	
		abreConn.OpenDB();
	}

	public void loadDataAlumno(String query) {
		this.query = query;
		conn = abreConn.getConnection();
		
		try {
			pstmt = conn.createStatement();
			rs = pstmt.executeQuery(query);		
			
			
			while (rs.next()) {
				int idAlumno = rs.getInt(1);			
				String nombre = rs.getString(2);
				String apellido1 = rs.getString(3);
				String apellido2 = rs.getString(4);
				String nif = rs.getString(5);
				String dir_alumno = rs.getString(6);
				String tfno_alumno = rs.getString(7);
				String fechaNac = rs.getString(8);
				String email_alumno = rs.getString(9);
				setAlumno(new AlumnoDAO(idAlumno, nombre, apellido1, apellido2,
						nif, dir_alumno, tfno_alumno, fechaNac, email_alumno)); //DAO en array?

			}

		} catch (SQLException ex) {
			Logger.getLogger(ManageAlumno.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			try {
				rs.close();
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	public void updateAlumno(String query) {
		this.query = query;

		conn = abreConn.getConnection();

		try {

			pstmt = conn.prepareStatement(query);
			pstmt.execute(query);
		} catch (SQLException ex) {
			Logger.getLogger(ManageAlumno.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {

			try {

				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	

	public void deleteAlumno(String query) {
		this.query = query;

		conn = abreConn.getConnection();

		try {

			pstmt = conn.prepareStatement(query);
			pstmt.execute(query);
		} catch (SQLException ex) {
			Logger.getLogger(ManageAlumno.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {

			try {

				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public String getOldQuery() {
		return query;
	}

	public Vector<AlumnoDAO> getAlumno() {
		return alumnos;
	}

	public void setAlumno(AlumnoDAO alumno) {
		this.alumnos.add(alumno);
	}

}
